package edu.sctu.zhangwang.month201506.day20150615;

import java.io.*;
import java.util.Scanner;

/**
 * Created by user10 on 2015/6/15.
 */
public class Myself {
    File file;
    public final String path = "D://";
    public void start(){
        int x = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("****--文件处理小程序--****");
        System.out.println("亲，请选择你需要的操作：");
        System.out.println("1、创建文件");
        System.out.println("2、删除文件");
        System.out.println("3、文本内容添加");
        System.out.println("4、复制该文件");
        System.out.println("5、移动该文件");
        System.out.println("6、");
        x = scanner.nextInt();
        switch (x){
            case 1: {
                creatFile();
                break;
            }
            case 2:{
                deleteFile();
                break;
            }
            case 3:{
                inputText();
                break;
            }
            case 4:{
                fileCopy();
                break;
            }
        }
    }

    /**
     * 创建文本
     * @return
     */
    public File creatFile(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入创建文件的文件名和拓展名。");
        String path = scanner.next();
        file = new File(this.path+path);
        try {
            if (!file.exists()) {
                file.createNewFile();
                System.out.println("创建成功！");
            }else{
                System.out.println("文件已存在！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 删除文本
     */
    public void deleteFile(){
        Scanner scanner = new Scanner(System.in);
        String path = scanner.next();
        file = new File(this.path+path);
        if (file.exists()){
        System.out.println("请选择要删除了文件");
        file.delete();
        }else{
            System.out.println("文件不存在。");
        }
    }

    /**
     * 文本输入操作
     */
    public void inputText(){
        try {
            Scanner scanner = new Scanner(System.in);
            FileOutputStream fileOutputStream = new FileOutputStream(creatFile());
            OutputStreamWriter writer = null;
            try {
                writer = new OutputStreamWriter(fileOutputStream,"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            System.out.println("输入需要输入的文本内容：");
            String outq = scanner.next();
            try {
                writer.append(outq);
                writer.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 文件复制
     */
    public void fileCopy(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件。");
//        String path = scanner.next();
//        file = new File(this.path+path);
        File file1 = new File("D://");
        String ming = scanner.next();
        String zi = scanner.next();
        try {
            File.createTempFile(ming,zi,file1);
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
